package org.gcb.core.ui.nodes;

import java.beans.IntrospectionException;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import org.gcb.core.model.Skill;
import org.gcb.core.model.SkillSet;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Jason
 */
public class SkillChildFactory extends ChildFactory<Skill>{
    static final Comparator<Skill> mapComparator = new Comparator<Skill>() {

        @Override
        public int compare(Skill o1, Skill o2) {
            return o1.getText().compareTo(o2.getText());
        }
    };
    private TreeSet<Skill> setSkills;

    public SkillChildFactory(TreeSet<Skill> skillSet) {
        this.setSkills = skillSet;
    }

    public SkillChildFactory(SkillSet skillSet) {
        this.setSkills = new TreeSet<Skill>(mapComparator);
        this.setSkills.addAll(skillSet.values());
    }

    @Override
    protected boolean createKeys(List<Skill> toPopulate) {
        for (Skill skill : setSkills) {
            toPopulate.add(skill);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(Skill key) {
        Node node;
        try {
            node = new BeanNode(key);
        } catch (IntrospectionException ex) {
            node = new AbstractNode(Children.LEAF, Lookups.singleton(key));
        }
        node.setDisplayName(key.getText() + " ("+key.getModifier()+')');
        node.setShortDescription(key.getDescription());
        return node;
    }

}
